﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using CodeGenerator.Core;

namespace CodeGenerator.UI
{
    public partial class FrmMain : Form
    {
        private FrmDataBaseInfo toolForm = new FrmDataBaseInfo();
        private FrmCustomedTemplate templateForm = new FrmCustomedTemplate();
        private FrmSystemTemplate systemTemplateForm = new FrmSystemTemplate();
        private FrmCenter frmCenter = new FrmCenter();

        public FrmMain()
        {
            InitializeComponent();

            templateForm.Show(this.dockPanel1, DockState.DockRightAutoHide);
            systemTemplateForm.Show(this.dockPanel1, DockState.DockRightAutoHide);
            this.ShowContent(frmCenter.Text, frmCenter);


            toolForm.UpdateTableInfoEvent += toolForm_UpdateTableInfoEvent;
            toolForm.Show(this.dockPanel1, DockState.DockLeft);
            toolForm.Focus();
        }

        void toolForm_UpdateTableInfoEvent(Core.ServerInfo server, string dbName, string tableName)
        {
            if (!string.IsNullOrEmpty(dbName))
            {
                tssl_currentTable.Text = server.Alias + "|" + dbName;
            }
            else
            {
                tssl_currentTable.Text = server.Alias;
            }
        }

        #region Method
        #region Tool
        /// <summary>
        /// 关闭所有文档
        /// </summary>
        private void CloseAllDocuments()
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    form.Close();
                }
            }
            else
            {
                IDockContent[] documents = dockPanel1.DocumentsToArray();
                foreach (IDockContent content in documents)
                {
                    content.DockHandler.Close();
                }
            }
        }

        /// <summary>
        /// 关闭其他窗体
        /// </summary>
        private void CloseOtherDocuments()
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
            {
                Form activeMdi = ActiveMdiChild;
                foreach (Form form in MdiChildren)
                {
                    if (form != activeMdi)
                    {
                        form.Close();
                    }
                }
            }
            else
            {
                foreach (IDockContent document in dockPanel1.DocumentsToArray())
                {
                    if (!document.DockHandler.IsActivated)
                    {
                        document.DockHandler.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 根据标题查找Document
        /// </summary>
        /// <param name="text">窗体标题</param>
        /// <returns></returns>
        private DockContent FindDocument(string text)
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }

                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel1.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }

        /// <summary>
        /// 打开文档，现根据caption查找Document，找到则激活，否则创建
        /// </summary>
        /// <param name="caption">文档标题</param>
        /// <param name="formType">窗体类型</param>
        /// <param name="reOpen">重新打开文档</param>
        /// <returns></returns>
        public DockContent ShowContent(string caption, DockContent form, bool reOpen = false)
        {
            DockContent frm = FindDocument(caption);
            if (frm == null)
            {
                frm = form; // (DockContent)Activator.CreateInstance(formType);
                frm.Text = caption;
            }
            else if (frm != null && reOpen)
            {
                frm.Close();

                frm = form; // (DockContent)Activator.CreateInstance(formType);
                frm.Text = caption;
            }

            frm.Show(this.dockPanel1);
            frm.BringToFront();
            return frm;
        }
        #endregion
        #endregion

        #region 菜单
        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSetup frm = new FrmSetup();
            frm.ShowDialog();
        }

        private void 关于我们ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox1().ShowDialog();
        }

        private void 我要反馈ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=Dz4_Ozw-Pjg8Pz9Pfn4hbGBi");
        }

        //#region 代码生成
        //private void buildH代码生成ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
        //    {
        //        FrmCreateBuildHMulti frm = new FrmCreateBuildHMulti();
        //        frm.ShowDialog();
        //    }
        //    else
        //    {
        //        Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
        //    }
        //}

        //private void mybatis代码批量生成ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
        //    {
        //        FrmCreateMybatisMulti frm = new FrmCreateMybatisMulti();
        //        frm.ShowDialog();
        //    }
        //    else
        //    {
        //        Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
        //    }
        //}

        //private void 实体类属性生成ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
        //    {
        //        FrmCreateEntityMulti frm = new FrmCreateEntityMulti();
        //        frm.ShowDialog();
        //    }
        //    else
        //    {
        //        Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
        //    }
        //}

        private void 数据库文档生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                FrmDict frm = new FrmDict();
                frm.ShowDialog();
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
            }
        }
        //#endregion

        //#region 在线工具
        //private void 在线文本比较工具ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Process.Start("explorer.exe", "http://www.jq22.com/textDifference");
        //}

        //private void jSON校验格式化工具ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Process.Start("explorer.exe", "http://www.bejson.com/");
        //}

        //private void 工具大全ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Process.Start("explorer.exe", "https://tool.lu/");
        //}

        //private void 在线图片压缩ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Process.Start("explorer.exe", "https://www.tiomg.org/image");
        //}
        //#endregion
        #endregion




        //private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Process.Start("http://www.shengyisoft.com");
        //}

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        #region 工具条
        
        // 配置信息
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            FrmSetup frm = new FrmSetup();
            frm.ShowDialog();
        }

        // 数据字典
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                FrmDict frm = new FrmDict();
                frm.ShowDialog();
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
            }
        }

        //// BuildH代码生成
        //private void toolStripButton3_Click(object sender, EventArgs e)
        //{
        //    if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
        //    {
        //        FrmCreateBuildHMulti frm = new FrmCreateBuildHMulti();
        //        frm.ShowDialog();
        //    }
        //    else
        //    {
        //        Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
        //    }
        //}

        //// MyBatis代码
        //private void toolStripButton4_Click(object sender, EventArgs e)
        //{
        //    if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
        //    {
        //        FrmCreateMybatisMulti frm = new FrmCreateMybatisMulti();
        //        frm.ShowDialog();
        //    }
        //    else
        //    {
        //        Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
        //    }
        //}



        // 实体属性
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                FrmCreateEntityMulti frm = new FrmCreateEntityMulti();
                frm.ShowDialog();
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
            }
        }
        #endregion

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                FrmCreateSYMulti frm = new FrmCreateSYMulti();
                frm.ShowDialog();
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
            }
        }

        private void shengYi代码批量生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Constant.CurrentServerInfo != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                FrmCreateSYMulti frm = new FrmCreateSYMulti();
                frm.ShowDialog();
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
            }
        }
    }
}
